// pages/demo/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    books: [],
    keywords: '',
    history: wx.getStorageSync('history') || []
  },
  // 获取图书
  getBooks() {
    // 利用 wx.request发起网络请求 获取数据
    wx.showLoading({
      title: '客官别急...',
      mask: true
    })
    wx.request({
      url: 'https://hmajax.itheima.net/api/books',
      method: 'GET',
      data: {
        creator: 'zhangsan'
      },
      success: (res) => {
        console.log(res);
        this.setData({
          books: res.data.data
        })
        // 给个轻提示
        wx.showToast({
          title: '获取成功了',
          mask: true,
          icon: 'none'
        })
      },
      complete: () => {
        wx.hideLoading()
      }
    })
  },
  setDataFn() {
    // wx.setStorageSync('msg', '我是个普通的msg')
    // 小程序的storage可以存放复杂类型
    wx.setStorageSync('obj', {name: '老韩', age: 20})
  },
  getData() {
    const data = wx.getStorageSync('obj')
    console.log(data);
  },
  removeData() {
    wx.removeStorageSync('obj')
  },
  clearData() {
    wx.clearStorageSync()
  },
  takePhoto() {
    wx.chooseMedia({
      count: 9,
      mediaType: ['image','video'],
      sourceType: ['album', 'camera'],
      maxDuration: 30,
      camera: 'back',
      success(res) {
        console.log(res)
        // console.log(res.tempFiles.size)
      }
    })
  },
  changeValue(e) {
    console.log(e);
    this.setData({
      keywords: e.detail.value
    })
  },
  search() {
    const {keywords, history} = this.data
    // 存keywords到history中
    history.push(keywords)
    wx.setStorageSync('history', history)
    this.setData({
      history,
      keywords: ''
    })
  },
  removeHistoryFn() {
    wx.removeStorageSync('history')
    this.setData({
      history: [],
      keywords: ''
    })
  }
})